Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add conversion api #89

Merged
merged 9 commits into from
Jul 23, 2021
Merged

Conversation

fubuloubu
Copy link
Member

@fubuloubu fubuloubu commented Jul 13, 2021

What I did

Added support for argument conversions via API

fixes: #3
depends: #92

How I did it

Added ConversionManager and ConverterAPI

How to verify it

(In ape console):

>>> convert("uint256", "1 ETH") == int(1e18)
True

Checklist

@fubuloubu
Copy link
Member Author

fubuloubu commented Jul 13, 2021

Python 3.6 bug w/ dataclassy, see: biqqles/dataclassy#52


Edit: deprecated Python 3.6 support in #100

@fubuloubu fubuloubu force-pushed the feat/conversion-api branch 6 times, most recently from 4fb4b2d to a7313bd Compare July 16, 2021 02:43
@fubuloubu fubuloubu force-pushed the feat/conversion-api branch 3 times, most recently from 2c4e445 to a80a3d6 Compare July 22, 2021 19:26
@fubuloubu fubuloubu closed this Jul 22, 2021
@fubuloubu fubuloubu reopened this Jul 22, 2021
@fubuloubu fubuloubu force-pushed the feat/conversion-api branch from a80a3d6 to 88f7dc3 Compare July 22, 2021 19:56
@fubuloubu fubuloubu force-pushed the feat/conversion-api branch from 90cc548 to 1d23f82 Compare July 23, 2021 20:33
@sabotagebeats
Copy link
Contributor

I was trying to verify it but I got the following error:

In [2]: convert("uint256", "1 ETH") == int(1e18)
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-2-51251206c2a7> in <module>
----> 1 convert("uint256", "1 ETH") == int(1e18)

~/work/apeworx/ape/src/ape/managers/converters.py in convert(self, value, type)
     83         if type not in self._converters:
     84             converter_types = ", ".join(map(lambda t: t.__name__, self._converters))
---> 85             raise Exception(f"ABI Type '{type}' must be one of [{converter_types}]")
     86
     87         if self.is_type(value, type):

Exception: ABI Type '1 ETH' must be one of [ChecksumAddress, bytes, int]

@sabotagebeats
Copy link
Contributor

I put a breakpoint at 83:

(Pdb) type
'1 ETH'
(Pdb) type in (self._converters)
False

@sabotagebeats
Copy link
Contributor

In [1]: convert("1 Eth",int) == int(1e18)
Out[1]: True

In [2]: convert("1 Eth",int)
Out[2]: 1000000000000000000

@fubuloubu
Copy link
Member Author

I was trying to verify it but I got the following error:

I swapped the order of the two, does it work now?

Copy link
Contributor

@sabotagebeats sabotagebeats left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fubuloubu no i think it has to be int not "uint256"

I swapped the order of the two, does it work now?

In [1]: convert("1 Eth","int") == int(1e18)
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-1-0c519f26f9f1> in <module>
----> 1 convert("1 Eth","int") == int(1e18)

~/work/apeworx/ape/src/ape/managers/converters.py in convert(self, value, type)
     83         if type not in self._converters:
     84             converter_types = ", ".join(map(lambda t: t.__name__, self._converters))
---> 85             raise Exception(f"ABI Type '{type}' must be one of [{converter_types}]")
     86
     87         if self.is_type(value, type):

Exception: ABI Type 'int' must be one of [ChecksumAddress, bytes, int]

src/ape/managers/converters.py Show resolved Hide resolved
unit=st.sampled_from(list(ETHER_UNITS.keys())),
)
def test_ether_conversions(value, unit):
assert convert(value=f"{value} {unit}", type=int) == int(value * ETHER_UNITS[unit])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test looks right, and passes

@sabotagebeats
Copy link
Contributor

@fubuloubu ok looks good to me as long as those caveats I listed above -

to use syntax needs to be like convert("1 Eth",int)

@sabotagebeats sabotagebeats merged commit 2efad19 into ApeWorX:main Jul 23, 2021
@fubuloubu fubuloubu deleted the feat/conversion-api branch July 24, 2021 16:31
@fubuloubu fubuloubu restored the feat/conversion-api branch July 24, 2021 16:58
fubuloubu added a commit that referenced this pull request Jul 24, 2021
* feat: add non-checksummed address converter (with warning)
* refactor: use internal AddressType instead of ChecksumAddress
* feat: add AddressAPI converter to address
* refactor: add conversion manager dep to account manager
* refactor: display accounts in repr
* feat: added support for ether types in account calls
* test: add tests for ether conversion fn
* feat: add conversion for Ether units
* feat: add ConversionAPI
* fix: updates eth-account requirement in setup.py to fix breaking change from pull 103 (#107)

Co-authored-by: Doggo <[email protected]>
@fubuloubu fubuloubu deleted the feat/conversion-api branch July 24, 2021 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Argument Conversion API
2 participants